package com.adong.juc;

import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

/**
 * 线程通信
 * 现在两个线程
 * 操作初始值为0的变量
 * 一个线程对变量进行加1,一个线程对变量减1
 * 交替来10轮
 */
public class NotifyWaitDemo {

    public static void main(String[] args) {

        /*ShareDataOne01 shareDataOne01 = new ShareDataOne01();

        new Thread(() -> {
            for (int i = 0; i < 10; i++) {
                try {
                    shareDataOne01.incr();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }

            }
        }, "AA").start();

        new Thread(() -> {
            for (int i = 0; i < 10; i++) {
                try {
                    shareDataOne01.decr();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }

            }
        }, "BB").start();*/

        ShareDataOne02 shareDataOne02 = new ShareDataOne02();

        new Thread(() -> {
            for (int i = 0; i < 10; i++) {
                try {
                    shareDataOne02.incr();
                } catch (Exception e) {
                    e.printStackTrace();
                }

            }
        }, "AA").start();

        new Thread(() -> {
            for (int i = 0; i < 10; i++) {
                try {
                    shareDataOne02.decr();
                } catch (Exception e) {
                    e.printStackTrace();
                }

            }
        }, "BB").start();


    }
}

class ShareDataOne01 {
    private int number = 0;

    public synchronized void incr() throws InterruptedException {
        // 判断
        while (number != 0) {
            this.wait();
        }
        // 干活
        number++;
        System.out.println(Thread.currentThread().getName() + "\t" + number);
        // 通知
        this.notifyAll();
    }

    public synchronized void decr() throws InterruptedException {
        // 判断
        while (number != 1) {
            this.wait();
        }
        // 干活
        number--;
        System.out.println(Thread.currentThread().getName() + "\t" + number);
        // 通知
        this.notifyAll();
    }

}

class ShareDataOne02 {

    private int number = 0;

    private Lock lock = new ReentrantLock();

    private Condition cd = lock.newCondition();


    public void incr() {
        lock.lock();
        try {
            // 判断
            while (number != 0) {
                cd.await();
            }
            // 干活
            number++;
            System.out.println(Thread.currentThread().getName() + "\t" + number);
            // 通知
            cd.signalAll();

        } catch (Exception e) {

        } finally {
            lock.unlock();

        }

    }

    public void decr() {
        lock.lock();
        // 判断
        try {
            while (number != 1) {
                cd.await();
            }
            // 干活
            number--;
            System.out.println(Thread.currentThread().getName() + "\t" + number);
            // 通知
            cd.signalAll();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            lock.unlock();
        }
    }

}

